package com.eleventwelve.iris.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.opentides.util.StringUtil;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.eleventwelve.iris.entity.Treatment;
import com.eleventwelve.iris.service.TreatmentService;

/**
 * 
 * @author Ashley
 *
 */
public class TreatmentDetailsController extends AbstractController {
	private String viewName;
	private TreatmentService treatmentService;
	private String preview;
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String viewType = "";
		Map<String, Object> map = new HashMap<String, Object>();
		
		String treatmentId = request.getParameter("id");
		Treatment treatment = null;
		if(!StringUtil.isEmpty(treatmentId) && StringUtils.isNumeric(treatmentId)){
			treatment = treatmentService.load(treatmentId);
		}
		
		if(treatment!=null){
			map.put("treatment", treatment);
			
			String previewParam = request.getParameter("peak");
			
			if(previewParam!=null && previewParam.equalsIgnoreCase("true")){
				viewType = this.preview;
			} else {
				viewType = viewName;
			}
			 
			return new ModelAndView(viewType, map);
		} else {
			//check if you are loading from a preview
			return new ModelAndView(viewName);	//this should be the error page
		}
		
	}


	public String getViewName() {
		return viewName;
	}


	public void setViewName(String viewName) {
		this.viewName = viewName;
	}


	public TreatmentService getTreatmentService() {
		return treatmentService;
	}


	public void setTreatmentService(TreatmentService treatmentService) {
		this.treatmentService = treatmentService;
	}


	public String getPreview() {
		return preview;
	}


	public void setPreview(String preview) {
		this.preview = preview;
	}

}
